package com.njbw.design_pattern.test.created.singleton;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 线程级别的单例
 *
 * @author 黄侯淳
 * @since JDK 1.8
 */
public class Connection {

    private static final ConcurrentHashMap<Long, Connection> instanceMap = new ConcurrentHashMap<>();

    private Connection() {}

    public static Connection getConnection() {
        long currentThreadId = Thread.currentThread().getId();
        instanceMap.putIfAbsent(currentThreadId, new Connection());
        return instanceMap.get(currentThreadId);
    }
}
